package com.aruba.paging3application.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.cachedIn
import com.aruba.paging3application.paging.MoviePagingSource

/**
 * Created by aruba on 2021/9/22.
 */
class MovieViewModel : ViewModel() {
    private val pagerFlow by lazy {
        Pager(config = PagingConfig(
            initialLoadSize = MoviePagingSource.pageSize * 2,//初次加载的数据量大小
            pageSize = MoviePagingSource.pageSize,
            enablePlaceholders = false
        ),
            pagingSourceFactory = {
                MoviePagingSource()
            }
        ).flow.cachedIn(viewModelScope)
    }

    //绑定page的flow，返回是一个flow对象
    fun bindPage() = pagerFlow
}